﻿using MetroDataQuery.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;

namespace MetroDataQuery.Utility
{
    public class PointsDictionaryQuery
    {
        /// <summary>
        /// 按照工程id和监测类型以及位置查询所有点名
        /// </summary>
        /// <param name="prjId">工程ID</param>
        /// <param name="monitorType">监测类型</param>
        /// <param name="location"></param>
        /// <returns>点名的列表</returns>
        public static List<string> GetPointNames(int prjId, string monitorType
            , string location)
        {
            return PointsDictionaryQuery.GetPointsDictonary(prjId, monitorType, location)
                .Select(p => p.Name).ToList();

        }

        /// <summary>
        /// 获取适合点名的sql查询数组
        /// </summary>
        /// <param name="prjId"></param>
        /// <param name="monitorType"></param>
        /// <param name="location"></param>
        /// <returns></returns>
        public static string GetPointNamesIntoSqlArray(int prjId, string monitorType, string location)
        {
            var names = PointsDictionaryQuery.GetPointNames(prjId, monitorType, location);
            names = names.Select(t => "'" + t + "'").ToList();
            return string.Join(",", names);
        }


        /// <summary>
        /// 按照工程id和监测类型以及位置查询所有配置点
        /// </summary>
        /// <param name="prjId">工程ID</param>
        /// <param name="monitorType">监测类型</param>
        /// <param name="location"></param>
        /// <returns>配置点的列表</returns>
        public static List<MetroDataQuery.Model.MonitorPointsDictionary> GetPointsDictonary(int prjId, string monitorType
            , string location = "")
        {
            using (var prjDb = new MetroDataQuery.Model.MetroProjectsEntities())
            {
                if (location == null || location == "")
                {
                    return prjDb.MonitorPointsDictionary.Where(p => p.PrjId == prjId
                         && p.MonitorType == monitorType)
                         .ToList();
                }
                else
                {
                    return prjDb.MonitorPointsDictionary.Where(p => p.PrjId == prjId
                                && p.MonitorType == monitorType && p.Location == location)
                                .ToList();
                }
            }
        }

        /// <summary>
        /// 获取点的配置信息
        /// </summary>
        /// <param name="prjId">工程id</param>
        /// <param name="ptName">点名</param>
        /// <returns>点的配置信息集</returns>
        public static MonitorPointsDictionary GetDictPointByName(int prjId, string ptName)
        {
            using (var prjDb = new MetroDataQuery.Model.MetroProjectsEntities())
            {
                var dict = prjDb.MonitorPointsDictionary.Where(p => p.PrjId == prjId && p.Name == ptName).SingleOrDefault();
                return dict;
            }
        }


        /// <summary>
        /// 获取点的配置信息
        /// </summary>
        /// <param name="prjId">工程id</param>
        /// <param name="ptName">点名</param>
        /// <returns>点的配置信息集</returns>
        public static List<MonitorPointsDictionary> GetDictPointByNameList(int prjId, List<string> nameList)
        {
            using (var prjDb = new MetroDataQuery.Model.MetroProjectsEntities())
            {
                var dicts = prjDb.MonitorPointsDictionary.Where(p => p.PrjId == prjId && nameList.Contains(p.Name)).ToList();
                var group = from p in dicts
                            group p by new { p.MonitorType, p.Location } into gs
                            select new MonitorPointsDictionary()
                            {
                                Location = gs.Key.Location,
                                MonitorType = gs.Key.MonitorType
                            };
                return group.ToList();
            }
        }
    }
}
